package com.cdck.safe.dete.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdck.safe.dete.model.entity.CommunityUser;
import com.cdck.safe.dete.model.entity.StudentUnionUser;
import com.cdck.safe.dete.mapper.StudentUnionUserMapper;
import com.cdck.safe.dete.model.entity.SysUser;
import com.cdck.safe.dete.model.query.BaseKeywordsQuery;
import com.cdck.safe.dete.model.query.StudentUnionUserQuery;
import com.cdck.safe.dete.model.vo.CommunityUserVo;
import com.cdck.safe.dete.model.vo.StudentUnionUserVo;
import com.cdck.safe.dete.service.StudentUnionUserService;
import com.cdck.safe.dete.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (StudentUnionUser)表服务实现类
 *
 * @author wp
 * @since 2024-02-26 23:02:29
 */
@Service
@RequiredArgsConstructor
public class StudentUnionUserServiceImpl extends ServiceImpl<StudentUnionUserMapper, StudentUnionUser> implements StudentUnionUserService {

    private  final SysUserService sysUserService;
    /**
     * 通过ID查询单条数据
     */
    @Override
    public StudentUnionUser getStudentUnionUserFormData(Long unionId, Long userId) {
        LambdaQueryWrapper<StudentUnionUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StudentUnionUser::getUnionId, unionId)
                .eq(StudentUnionUser::getUserId, userId);
        return this.getOne(wrapper);
    }

    /**
     * 分页查询
     *
     * queryParams 筛选条件
     * @return 查询结果
     */
    @Override
    public IPage<StudentUnionUserVo> queryByPage(StudentUnionUserQuery queryParams){
        if (Objects.equals(queryParams.getUnionId(),null)){
            throw  new RuntimeException("学生会Id不能为空");
        }
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        String keywords = queryParams.getKeywords();
        Page<StudentUnionUser> page = new Page<>(pageNum, pageSize);
        this.page(
                page,
                new LambdaQueryWrapper<StudentUnionUser>()
                        .eq(StudentUnionUser::getUnionId,queryParams.getUnionId())
                        .eq(StudentUnionUser::getStatus,queryParams.getStatus())
        );
        List<StudentUnionUserVo> studentUnionUserVos = page.getRecords().stream().map(item -> {
            StudentUnionUserVo studentUnionUserVo = new StudentUnionUserVo();
            SysUser user = sysUserService.getById(item.getUserId());
            BeanUtil.copyProperties(user, studentUnionUserVo);
            studentUnionUserVo.setUnionUserId(item.getUnionUserId());
            studentUnionUserVo.setUnionId(queryParams.getUnionId());
            return studentUnionUserVo;
        }).collect(Collectors.toList());

        Page pageInfo = new Page(pageNum, pageSize);
        BeanUtil.copyProperties(page,pageInfo,"records");
        pageInfo.setRecords(studentUnionUserVos);
        return pageInfo;
     }

    /**
     * 新增数据
     *
     * @param studentUnionUser 实例对象
     * @return 实例对象
     */
    @Override
    public boolean saveStudentUnionUser(StudentUnionUser studentUnionUser) {
       return this.save(studentUnionUser);
    }

    /**
     * 修改数据
     *
     * @param studentUnionUser 实例对象
     * @return 实例对象
     */
    @Override
    public boolean updateStudentUnionUser(Long id, StudentUnionUser studentUnionUser) {
         return this.updateById(studentUnionUser);
    }

    /**
     * 通过主键删除数据
     *
     * @param idsStr ids集合
     * @return 是否成功
     */
    @Override
    public boolean deleteStudentUnionUsers(String idsStr) {
         Assert.isTrue(StrUtil.isNotBlank(idsStr), "删除的数据为空");
        // 逻辑删除
        List<Long> ids = Arrays.stream(idsStr.split(","))
                .map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(ids);
    }

    @Override
    public boolean updateStatusUnionUser(Long id) {
        StudentUnionUser studentUnionUser = this.getById(id);
        studentUnionUser.setStatus(1);
        return this.updateById(studentUnionUser);
    }
}
